package org.springblade.mes.log.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.mes.log.model.SyncHistoryBean;
import org.springblade.mes.log.service.SyncHistoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 控制器
 *
 * @author qp
 */
@Slf4j
@RestController
@RequestMapping("/syncHistory")
public class SyncHistoryController extends BladeController {

	@Resource
	private SyncHistoryService syncHistoryService;

	/**
	 * 新增接口
	 */
	@PostMapping("/save")
	public R<String> save(@RequestBody SyncHistoryBean param) {
		try {
			if(this.syncHistoryService.addData(param)){
    			return R.success("保存成功");
			}
    		return R.fail("保存失败");
		} catch (Exception ex) {
    		log.error("保存异常，" + ex.getMessage(), ex);
			return R.fail(ex.getMessage());
		}
	}

	/**
	 * 编辑接口
	 */
	@PostMapping("/edit")
	public R<String> edit(@RequestBody SyncHistoryBean param) {
		try {
			if(this.syncHistoryService.updateData(param)){
				return R.success("修改成功");
			}
			return R.fail("修改失败");
		} catch (Exception ex) {
        	log.error("修改异常，" + ex.getMessage(), ex);
        	return R.fail(ex.getMessage());
		}
	}

	/**
	 * 删除接口
	 */
	@PostMapping("/remove")
	public R<String> remove(@RequestBody SyncHistoryBean param) {
		try {
            this.syncHistoryService.deleteData(param);
            return R.success("删除成功");
		} catch (Exception ex) {
            log.error("删除异常，" + ex.getMessage(), ex);
        	return R.fail(ex.getMessage());
		}
	}

	/**
	 * 查看详情接口
	 */
	@GetMapping("/detail")
	public R<SyncHistoryBean> detail(SyncHistoryBean param) {
		try{
			SyncHistoryBean detail = this.syncHistoryService.findBySpec(param);
			return R.data(detail);
		} catch (Exception ex) {
			log.error("查询异常，" + ex.getMessage(), ex);
			return R.fail(ex.getMessage());
		}
	}

	/**
	 * 查询分页
	 */
	@GetMapping("/page")
	public R<IPage<SyncHistoryBean>> page(SyncHistoryBean param, Query query) {
		try{
			IPage<SyncHistoryBean> page = this.syncHistoryService.findPageBySpec(Condition.getPage(query), param);
			return R.data(page);
		} catch (Exception ex) {
			log.error("查询异常，" + ex.getMessage(), ex);
			return R.fail(ex.getMessage());
		}
	}

	/**
	 * 查询列表
	 */
	@GetMapping("/list")
	public R<List<SyncHistoryBean>> list(SyncHistoryBean param) {
		try {
			List<SyncHistoryBean> dataList = this.syncHistoryService.findListBySpec(param);
			return R.data(dataList);
		} catch (Exception ex) {
			log.error("查询异常，" + ex.getMessage(), ex);
			return R.fail(ex.getMessage());
		}
	}
}

